// 实现这个项目的构建任务
const del = require('del')
const browser = require('browser-sync')
const bs = browser.create()

const { src, dest, parallel, series, watch } = require('gulp')
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()


const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const clear = () => {
  return del(['dist'])
}

const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: "expanded" }))
    .pipe(dest('dist/dev'))
    .pipe(bs.reload({ stream: true }))
}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('dist/dev'))
    .pipe(bs.reload({ stream: true }))
}

const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data }))
    .pipe(dest('dist/dev'))
    .pipe(bs.reload({ stream: true }))
}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist/pro'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist/pro'))
}

const extra = () => {
  return src('public/*', { base: 'public' })
    .pipe(dest('dist/pro'))
}

const useref = () => {
  return src('dist/dev/*.html', { base: 'dist/dev' })
    .pipe(plugins.useref({ searchPath: ['dist/dev', '.'] }))
    .pipe(plugins.if(/.js$/, plugins.uglify()))
    .pipe(plugins.if(/.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
    })))
    .pipe(dest('dist/pro'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/script/*.js', script)
  watch('src/*.html', page)
  watch(['src/assets/images/**', 'src/assets/fonts/**', 'public/*'], bs.reload)

  bs.init({
    notify: false, //右上角的图标
    port: '8989', //端口
    //files: 'dist/**', //变化的文件 或者在变化的函数后面加bs.reload
    server: {
      baseDir: ['dist/dev', 'src', 'public'], // 运行的文件路劲,从左到右依次查找
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const info = parallel(style, script, page)
//开发环境
const dev = series(info, serve)
//生产环境
const build = series(clear, parallel(series(info, useref), image, font, extra))

module.exports = {
  dev,
  clear,
  build
}
